package com.mzl.lal4cloud.lxw4rf.service.contract.http.okhttp;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mzl.lal4cloud.lxw4rf.service.contract.common.Consts;
import com.mzl.lal4cloud.lxw4rf.service.contract.exception.ErrorException;
import com.mzl.lal4cloud.lxw4rf.service.contract.http.RequestHttp;
import com.mzl.lal4cloud.lxw4rf.service.contract.http.SimplePostRequestExecutor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import java.io.IOException;

/**
 * @author J_luo
 */
@Slf4j
public class OkHttpSimplePostRequestExecutor extends SimplePostRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
    public OkHttpSimplePostRequestExecutor(RequestHttp requestHttp) {
        super(requestHttp);
    }

    @Override
    public String execute(String uri, String requestData) throws ErrorException, IOException {
        RequestBody body = RequestBody.create(Consts.JSON_TYPE, requestData == null ? "" : requestData);


        final Request.Builder requestBuilder = new Request
                .Builder()
                .url(uri)
                .post(body)
                .headers(this.getHeaders(uri, requestData));

        Response response = requestHttp.getRequestHttpClient().newCall(requestBuilder.build()).execute();
        return this.handleResponse(response);
    }

}
